﻿using System;
using System.Windows.Forms;
using TerraExplorerX;
using System.IO;
using System.Drawing;
using DevComponents.DotNetBar;
using SuperMapLib;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace HYWJ
{
    public partial class MianForm : Office2007Form
    {
        //定义全局变量保存用户的信息
        public  string UserName = "";
        
        //定义全局变量保存时间
        public string UserTime = "";

        //TerraExplorer
        private TerraExplorerClass TE;
        private ITerraExplorer51 ITerraExplorer;
        private IPlane5 IPlane;
        private IInformationTree5 IInformationTree;
        private ITerrainLabel5 ITlable;
        private IObjectManager51 Iobjtm;
        //private IObjectManager IobjtmTest;

        public int nStates = 0;
        bool skywithout = false;
        bool supwithout = false;
        int modaltype = 2;
        double ax = 9999, ay = 9999, ac = -1;
        public static bool liandong = false;
        static double mapx = 9999;
        static double mapy = 9999;
        static double ascale = -1;
        string custfilename = "";
        System.Windows.Forms.Timer gtimer = new System.Windows.Forms.Timer();
        private MarqueeLabel.Label label = null;
        

        public MianForm()
        {
            InitializeComponent();
            this.BindMapData();
        }

        #region  窗体事件

        /// <summary>
        /// 系统运行时加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MianForm_Load(object sender, EventArgs e)
        {
            this.timerHY.Enabled = true;
            TE.OnLoadFinished += new _ITerraExplorerEvents5_OnLoadFinishedEventHandler(TE_OnLoadFinished);
            ////this.toolStripStatusName.Text ="当前登录用户:"+UserName;
            panel2.Location = new Point(0, 0);
            panel2.Size = new Size(300, this.panelEx1.Height);
            ZMlabel();
        }

        /// <summary>
        /// 加载字幕label
        /// </summary>
        private void ZMlabel()
        {
            label = new MarqueeLabel.Label();
            label.Location = new Point(0, axTE3DWindow1.Height-100);
            label.Size = new Size(axTE3DWindow1.Width, 50);
            label.Show();
            label.Once = true;
            label.AutoSize = false;
            label.Visible = false;
            //tang
            //label.Transparency = 255;
            //axTE3DWindow1.Controls.Add(label);
            
        }

        void TE_OnLoadFinished()
        {
            this.axMGPSymbolTreeCtrl1.Connect(this.axMGPSymbolPanelCtrl1.ObjHandle);
            this.axMGPSymbolTreeCtrl1.Refresh();
            this.axSuperMap1.AfterGeometryAdded += new AxSuperMapLib._DSuperMapEvents_AfterGeometryAddedEventHandler(this.axSuperMap1_AfterGeometryAdded);
            this.axSuperMap1.OnCustomGeometryCreate += new AxSuperMapLib._DSuperMapEvents_OnCustomGeometryCreateEventHandler(this.axSuperMap1_OnCustomGeometryCreate);
            this.axSuperMap1.GeometrySelected += new AxSuperMapLib._DSuperMapEvents_GeometrySelectedEventHandler(this.axSuperMap1_GeometrySelected);
            axSuperMap1.AfterMapDraw += new AxSuperMapLib._DSuperMapEvents_AfterMapDrawEventHandler(axSuperMap1_AfterMapDraw);
            this.axSuperMap1.MouseWheelEvent += new AxSuperMapLib._DSuperMapEvents_MouseWheelEventHandler(axSuperMap1_MouseWheelEvent);
            this.axMGPPropList1.PropertyChanged += new AxMGPPROPLISTLib._DMGPPropListEvents_PropertyChangedEventHandler(this.axMGPPropList1_PropertyChanged);

            this.axMGPSymbolPanelCtrl1.OnClick += new AxMGPEXT20Lib._IMGPSymbolPanelCtrlEvents_OnClickEventHandler(this.axMGPSymbolPanelCtrl1_OnClick);

            gtimer.Interval = 500;
            gtimer.Tick += new EventHandler(gtimer_Tick);
            gtimer.Start();
            load2Dmap();
        }

        /// <summary>
        /// 系统时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerHY_Tick(object sender, EventArgs e)
        {
            this.da.Text = "当前时间:" + System.DateTime.Now.ToString();
        }

        /// <summary>
        /// 系统退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void buttonItem90_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 关于系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAbout_Click(object sender, EventArgs e)
        {
            About abo = new About();
            abo.Owner = this;
            abo.ShowDialog();
        }

        #region  三维数据工具箱

        /// <summary>
        /// 测量水平距离
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem7_Click(object sender, EventArgs e)
        {
            //测量工具箱
            this.TE.Invoke(33325);

        }

        /// <summary>
        /// 测量空间距离
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem20_Click(object sender, EventArgs e)
        {
            this.TE.Invoke(33327);

        }

        /// <summary>
        /// 测量垂直距离
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem22_Click(object sender, EventArgs e)
        {
            this.TE.Invoke(33330);

        }

        /// <summary>
        /// 测量面积
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem23_Click(object sender, EventArgs e)
        {
            this.TE.Invoke(33350);
        }


        /// <summary>
        /// 自动生成等高线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem24_Click(object sender, EventArgs e)
        {
            this.TE.Invoke(33331);

        }

        /// <summary>
        /// 地面剖面分析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem26_Click(object sender, EventArgs e)
        {
            this.TE.Invoke(33329);

        }

        /// <summary>
        /// 最优路径分析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem29_Click(object sender, EventArgs e)
        {
            this.TE.Invoke(33333);

        }

        /// <summary>
        /// 圆顶辐射分析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem32_Click(object sender, EventArgs e)
        {
            this.TE.Invoke(33332);

        }

        /// <summary>
        /// 视域分析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem31_Click(object sender, EventArgs e)
        {
            this.TE.Invoke(33334);
        }

        /// <summary>
        /// 视线分析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem30_Click(object sender, EventArgs e)
        {
            this.TE.Invoke(33334);
        }
        #endregion

        #endregion

        #region 自定义方法

        /// <summary>
        /// 经纬度表中为23-23-23转换成23°23´23"
        /// </summary>
        /// <param name="strJWDValue">经纬度</param>
        /// <returns>转换成度</returns>
        private string GetDuFenMiao(string strJWDValue)
        {
            string strValue = "";
            //地图上显示必须是23°23´23"
            string[] strJWDList = strJWDValue.Split('-');
            strValue = strJWDList[0].ToString() + "°" + strJWDList[1].ToString() + "′" + strJWDList[2].ToString()+"″";
            return strValue;
        }

        /// <summary>
        /// 经纬度表中为23-23-23转换成度
        /// </summary>
        /// <param name="strJWDValue">经纬度</param>
        /// <returns>转换成度</returns>
        private Double JwdToDu(string strJWDValue)
        {
            Double dValue = 0;
            //表中经纬度存为如： 23-23-23  
            string[] strJWDList = strJWDValue.Split('-');
            dValue = Convert.ToDouble(strJWDList[0]);
            dValue = dValue + Convert.ToDouble(strJWDList[1]) / 60;
            dValue = dValue + Convert.ToDouble(strJWDList[2]) / 3600;
            dValue = Convert.ToDouble(dValue.ToString("f6"));
            return dValue;
        }

        /// <summary>
        /// 初始化数据绑定
        /// </summary>
        private void BindMapData()
        {
            this.TE = new TerraExplorerClass();
            this.ITerraExplorer = (ITerraExplorer51)TE;
            this.IInformationTree = (IInformationTree5)TE;
            this.IPlane = (IPlane5)TE;
            //加载fly
            ITerraExplorer.LoadEx(Application.StartupPath + @"\沈阳mpt\Default.fly", "", "", 0);
            IPlane.FlyToObject(IInformationTree.FindItem("棋盘山开发区") + "", 0);
            size3D();
            //TE = new TerraExplorerClass();
            //string URL = Application.StartupPath + @"\沈阳mpt\Default.fly";
            //TE.Load(URL);
            //IPlane.FlyToObject(IInformationTree.FindItem("棋盘山开发区") + "", 0);
        }

        /// <summary>
        /// 在三维球上创建显示的lable
        /// </summary>
        /// <param name="dX">经度</param>
        /// <param name="dY">纬度</param>
        /// <param name="strInfo">显示信息</param>
        private void ShowGisLable(Double dX,Double dY,string strInfo)
        {
            this.Iobjtm = (IObjectManager51)TE;
            this.ITlable = Iobjtm.CreateLabel(null, dX, dY, 52, HeightStyleCode.HS_ON_TERRAIN, 0, "111");
            //字体样式
            ITlable.FontName = "Arial Rounded MT Bold";
            //字体颜色
            ITlable.FgColor = 0x990033;
            //显示文本
            ITlable.Text = "";
            ITlable.Text = strInfo;
            //字体尺寸
            ITlable.FontSize = 8;
            //比例
            ITlable.ScaleFactor = 1;
            //锁定模式
            ITlable.LockMode = 0;
            ITlable.FrameFileName = "";
            ITlable.LimitGrowth = 0;
            ITlable.MinimumViewingHeight = 0;
            ITlable.MaxVisibilityDistance = 1000;
            ITlable.TextOnImage = 0;
            //默认观察距离
            ITlable.Distance = 500;
            //提示信息
            var CurrentObjectMessage = Iobjtm.CreateMessage(MsgClient.CLIENT_POPUP,strInfo, MsgType.TYPE_TEXT, _HTML_POPUP_FLAGS.HTML_POPUP_ALLOW_DRAG, "信息提示", -1, 0, 0, 200, 120, 1);
            ITlable.MessageID = CurrentObjectMessage.ID;
            //飞行
            IPlane.FlyToObject(ITlable.ID, 0);

            //测试用数据
            // IPlane.FlyTo(dX,dY, 150000, 100, 90, 0, "FlyToTerrain");
            //"经度:110.2966 纬度:45.331" + Environment.NewLine + 
            //"编队名称:中南一队" + Environment.NewLine + "舰艇型号:ch-001" 
            //+ Environment.NewLine + "现有数量:1000";
        }


        private void axMGPPropList1_PropertyChanged(object sender, AxMGPPROPLISTLib._DMGPPropListEvents_PropertyChangedEvent e)
        {

            SuperMapLib.IsoSelection pCurrSel = null;
            pCurrSel = this.axSuperMap1.selection;

            SuperMapLib.IsoRecordset pRecordSet = null;
            pRecordSet = pCurrSel.ToRecordset(true);

            SuperMapLib.IsoGeometry pGeometry = null;
            pGeometry = pRecordSet.GetGeometry();

            SuperMapLib.IsoGeoCustom pGeoCustom = (SuperMapLib.IsoGeoCustom)pGeometry;
            //pGeoCustom.CustomObjInterface = this.axMGPPropList1.GetCurrentObject();

            SuperMapLib.soGeometry pSoGeo = null;
            pSoGeo = (SuperMapLib.soGeometry)pGeoCustom;
            pRecordSet.Edit();
            pRecordSet.SetGeometry(pSoGeo);
            pRecordSet.Update();

            SuperMapLib.soRect pRect = null;
            pRect = pSoGeo.Bounds;
            this.axSuperMap1.RefreshEx(pRect);
        }

        void load2Dmap()
        {

            string strPath = Path.Combine(Application.StartupPath, @"data\2dmap\China\china.smw");
            this.axSuperWorkspace1.Open(strPath);

            this.axSuperMap1.Connect(axSuperWorkspace1.CtlHandle);
            this.axSuperMap1.OpenMap("china");
            this.axSuperMap1.Action = SuperMapLib.seAction.scaSelectEx;
            string strImpFileName = System.Windows.Forms.Application.StartupPath + "\\suRes\\symlib.sym";

            soSymbolLib objSymbol = axSuperWorkspace1.Resources.SymbolLib;// '获取要转入的符号库
            objSymbol.Import(strImpFileName, false);
            
            axSuperMap1.ViewScale = 0.0000001 * 111000;
            axSuperMap1.Refresh();
            axSuperMap1.CenterX = 123.64;
            axSuperMap1.CenterY = 41.92;
            axSuperMap1.Refresh();

        }
        void axSuperMap1_AfterMapDraw(object sender, AxSuperMapLib._DSuperMapEvents_AfterMapDrawEvent e)
        {

            if (modaltype == 3)
            {

                double mx = axSuperMap1.CenterX;
                double my = axSuperMap1.CenterY;

                if (ax != mx || ay != my || ac != axSuperMap1.ViewScale)
                {
                    if (supwithout)
                    {
                        supwithout = !supwithout;
                    }
                    else
                    {
                        if (liandong)
                        {
                            ac = axSuperMap1.ViewScale;
                            ax = mx;
                            ay = my;
                            skywithout = true;
                            SetPosition(mx, my, axSuperMap1.ViewScale / 111000);
                        }

                    }
                }
            }
        }
        private void axMGPSymbolPanelCtrl1_OnClick(object sender, AxMGPEXT20Lib._IMGPSymbolPanelCtrlEvents_OnClickEvent e)
        {
            try
            {
                //{{获取鼠标所选取对象的信息，设置军标绘制状态
                MGPEXT20Lib.MGPSymbolPanelItem item = axMGPSymbolPanelCtrl1.SelectedItem;
                MGPEXT20Lib.mgePanelItemType type = item.Type;

                this.axSuperMap1.CurrentCustomGeometryLibrary = 2005;
                this.axSuperMap1.CurrentCustomGeometryType = (int)type;
                this.axSuperMap1.Action = SuperMapLib.seAction.scaEditCreateCustom;
                //}}获取鼠标所选取对象的信息，设置军标绘制状态
            }
            catch (Exception)
            {
            }

        }

        private void axSuperMap1_OnCustomGeometryCreate(object sender, AxSuperMapLib._DSuperMapEvents_OnCustomGeometryCreateEvent e)
        {
            //注意：所有com对象都需要释放，包括外面传进来的
            try
            {
                if (e.objCustomGeometry == null)
                {
                    return;
                }

                MGPEXT20Lib.ISmExtObj objEx = (MGPEXT20Lib.ISmExtObj)e.objCustomGeometry.CustomObjInterface;
                if (objEx == null)
                {
                    return;
                }

                MGPEXT20Lib.MGPDotSymbol dotSymbol;
                MGPEXT20Lib.MGPLineSymbol lineSymbol;
                MGPEXT20Lib.MGPText txtSymbol;
                MGPEXT20Lib.MGPDotSymbolEx dotySymbolEx;
                string filename = "";
                MGPEXT20Lib.MGPSymbolPanelItem item = axMGPSymbolPanelCtrl1.SelectedItem;
                //设置军标符号大小
                double dsize = 40;
                switch (objEx.Type)
                {
                    case 1://点军标
                        dotSymbol = (MGPEXT20Lib.MGPDotSymbol)objEx;
                        dotSymbol.LibID = item.LibID;
                        dotSymbol.Code = item.Code;
                        string name = item.Path;
                        string[] allsub = name.Split('\\');
                        int inum = allsub.GetLength(0);
                        custfilename = Application.StartupPath + @"\junbiao\" + allsub[inum - 2] + @"\" + allsub[inum - 1] + @"\" + item.Name + ".png";

                        dotSymbol.Width = 40;//dsize;
                        dotSymbol.Height = 40;//dsize;
                        dotSymbol.AnnotationHeight = 40;//ize / 2;
                        Marshal.ReleaseComObject(dotSymbol);
                        break;
                    case 2://线军标
                        lineSymbol = (MGPEXT20Lib.MGPLineSymbol)objEx;
                        lineSymbol.Code = item.Code;
                        Marshal.ReleaseComObject(lineSymbol);
                        break;

                    case 3://文本标注
                        txtSymbol = (MGPEXT20Lib.MGPText)objEx;
                        txtSymbol.Code = (Int16)item.Code;
                        txtSymbol.Height = dsize;
                        txtSymbol.Width = dsize;
                        txtSymbol.TextHeight = dsize / 2;
                        txtSymbol.TextWidth = dsize / 4;
                        Marshal.ReleaseComObject(txtSymbol);
                        break;

                    //// <summary>
                    ///   限制通通调整为点军标扩展符号，取消点军标符号，objEx.Type=1的情况废弃
                    /// </summary>
                    case 4: //点标号扩展
                        dotySymbolEx = (MGPEXT20Lib.MGPDotSymbolEx)objEx;
                        dotySymbolEx.LibID = item.LibID;
                        dotySymbolEx.Code = item.Code;
                        dotySymbolEx.Width = dsize;
                        dotySymbolEx.Height = dsize;
                        dotySymbolEx.AnnotationHeight = dsize / 2;
                        /* 设置点军标扩展符号对象是否包含子军标对象集合
                         * false:不包含，则等同点军标符号
                         * true:包含，为点军标扩展符号，设置子军标对象的属性
                         * 目前子军标对象支持状态栏、人员设备、行动能效三个子军标对象
                        [12/30/2008 supermap]
                         */
                        dotySymbolEx.HasChildDotSymbols = true;

                        //如果不包含子军标对象集合，则结束，直接画点军标符号
                        if (!dotySymbolEx.HasChildDotSymbols)
                        {
                            break;
                        }

                        //调用点军标扩展符号的三个接口，分别获取状态栏、人员设备、行动能效三个子军标对象
                        MGPEXT20Lib.MGPPie equipment = dotySymbolEx.GetEquipmentPerson();
                        MGPEXT20Lib.MGPPie actionEffi = dotySymbolEx.GetActionEfficiency();
                        MGPEXT20Lib.MGPStatusBar statusBar = dotySymbolEx.GetStatusBar();
                        //////////////////////////////////////////////////////////////////////////

                        //具体参数设置参考《扩展点军标对象使用参考.doc》文档 [12/30/2008 supermap]
                        if (equipment != null && actionEffi != null && statusBar != null)
                        {
                            equipment.Division = 2;
                            actionEffi.Division = 3;
                            actionEffi.SegmentStartAngle = 90;
                            statusBar.Division = 5;

                            int i = 0;
                            MGPEXT20Lib.structSEGMENTSTYLE statusBarStyle = new MGPEXT20Lib.structSEGMENTSTYLE();
                            MGPEXT20Lib.structSEGMENTSTYLE equipmentStyle = new MGPEXT20Lib.structSEGMENTSTYLE();
                            MGPEXT20Lib.structSEGMENTSTYLE actionEffiStyle = new MGPEXT20Lib.structSEGMENTSTYLE();

                            statusBarStyle.TextColor = ParseRGB(0, 0, 0);
                            statusBarStyle.FontName = "宋体";
                            statusBarStyle.Name = "Test";
                            statusBarStyle.text = "Test";
                            statusBarStyle.PenWidth = 3;
                            statusBarStyle.PenColor = ParseRGB(128, 128, 128);

                            equipmentStyle = statusBarStyle;
                            actionEffiStyle = statusBarStyle;

                            for (i = 0; i < statusBar.Division; i++)
                            {
                                if (i == 1)
                                {
                                    /*测试颜色设置
                                    style.BackColor = ParseRGB(189, 217, 198);
                                    style.FillColor = ParseRGB(230, 102, 102);
                                     * */
                                    statusBarStyle.FillColor = ParseRGB(228, 42, 152);
                                }
                                else
                                {
                                    /*测试颜色设置
                                    style.FillColor = ParseRGB(172,197,206);                           
                                    style.FillColor = ParseRGB(189, 217, 198);
                                     * */
                                    statusBarStyle.FillColor = ParseRGB(232, 223, 124);
                                }
                                statusBar.SetStyle(i, statusBarStyle);
                            }

                            for (i = 0; i < equipment.Division; i++)
                            {
                                /*测试颜色设置
                               style.FillColor = ParseRGB(22, 135, 240);
                               style.FillColor = ParseRGB(96,200,108);
                               style.BackColor = ParseRGB(189, 217, 198);
                               style.FillColor = ParseRGB(232, 223, 124);
                                 * */

                                equipmentStyle.FillColor = ParseRGB(199, 225, 55);
                                equipmentStyle.BackColor = ParseRGB(232, 223, 124);
                                if (i == 0)
                                {
                                    equipmentStyle.FillPercent = 60;
                                    equipmentStyle.FillDirection = 1;
                                }

                                if (i == 1)
                                {
                                    equipmentStyle.FillPercent = 30;
                                    equipmentStyle.FillDirection = 0;
                                }
                                equipment.SetStyle(i, equipmentStyle);
                            }

                            for (i = 0; i < actionEffi.Division; i++)
                            {
                                /*测试颜色设置
                               //style.FillColor = ParseRGB(22, 135, 240);
                               //style.FillColor = ParseRGB(96, 200, 108);
                               //style.BackColor = ParseRGB(189, 217, 198);                                                               
                               //style.FillColor = ParseRGB(232, 223, 124);
                               */
                                actionEffiStyle.FillDirection = 0;
                                actionEffiStyle.FillPercent = 100;
                                actionEffiStyle.FillColor = ParseRGB(199, 225, 55);
                                actionEffiStyle.BackColor = ParseRGB(232, 223, 124);
                                if (i == actionEffi.Division - 2)
                                {
                                    actionEffiStyle.FillPercent = 30;
                                }

                                if (i == actionEffi.Division - 1)
                                {
                                    actionEffiStyle.FillPercent = 0;
                                }
                                actionEffi.SetStyle(i, actionEffiStyle);
                            }
                        }
                        break;
                    default:
                        break;
                }
                //下面的代码释放com对象
                Marshal.ReleaseComObject(item);
                Marshal.ReleaseComObject(objEx);
                Marshal.ReleaseComObject(e.objCustomGeometry);

            }
            catch (Exception)
            {
            }

        }
        uint ParseRGB(int r, int g, int b)
        {
            return (uint)(r + g * 256 + b * 256 * 256);
        }

        private void axSuperMap1_AfterGeometryAdded(object sender, AxSuperMapLib._DSuperMapEvents_AfterGeometryAddedEvent e)
        {

            //this.axMGPPropList1.SetDefault();
            //this.axMGPPropList1.FillProperty(null);
            SuperMapLib.soSelection pCurrSel = this.axSuperMap1.selection;
            //{{
            int nCount = pCurrSel.Count;
            SuperMapLib.soRecordset pCurrRecordSet = pCurrSel.ToRecordset(false);
            //}}

            int nRecordCount = pCurrRecordSet.RecordCount;
            //对选中的一个对象进行属性显示修改
            if (nRecordCount == 1)
            {
                SuperMapLib.IsoGeometry pCurrGeo = null;
                pCurrGeo = pCurrRecordSet.GetGeometry();
                if (pCurrGeo.Type == SuperMapLib.seGeometryType.scgCustom)
                {
                    SuperMapLib.IsoGeoCustom pGeoCustom = (SuperMapLib.IsoGeoCustom)pCurrGeo;
                    if (File.Exists(custfilename))
                    {
                        TerraExplorerX.IObjectManager51 kk = TE as TerraExplorerX.IObjectManager51;
                        //object leb = new object ();
                        TerraExplorerX.LabelStruct sTr = new TerraExplorerX.LabelStruct();

                        double efx = pGeoCustom.Bounds.CenterPoint().x;
                        double efy = pGeoCustom.Bounds.CenterPoint().y;

                        TerraExplorerX.ITerrainLabel5 txtObj = kk.CreateLabel((object)sTr, efx, efy, 0, TerraExplorerX.HeightStyleCode.HSC_ON_TERRAIN, 0, "");

                        txtObj.FontName = "隶书";
                        txtObj.FgColor = 0xffffff;
                        txtObj.Text = "";
                        txtObj.FontSize = 28;
                        txtObj.ScaleFactor = 300;
                        txtObj.BgColor = 0x0c0c0c;
                        txtObj.FrameFileName = "";
                        txtObj.ImageFileName = custfilename;
                        ///txtObj.LimitGrowth = 0;
                        txtObj.MinimumViewingHeight = 0;
                        txtObj.MaxVisibilityDistance = 100000000000;
                        //txtObj.TextOnImage = 0;
                        //TE.CreateImageLabelEx(efx, efy, 0, filename, 0, "");
                    }
                }
            }
            FillProperty();

            this.axSuperMap1.Action = seAction.scaSelectEx;
        }
        private void axSuperMap1_GeometrySelected(object sender, AxSuperMapLib._DSuperMapEvents_GeometrySelectedEvent e)
        {
            FillProperty();

        }
        private void FillProperty()
        {
            try
            {
                SuperMapLib.soSelection pCurrSel = this.axSuperMap1.selection;
                //{{
                int nCount = pCurrSel.Count;
                SuperMapLib.soRecordset pCurrRecordSet = pCurrSel.ToRecordset(false);
                //}}

                int nRecordCount = pCurrRecordSet.RecordCount;
                //对选中的一个对象进行属性显示修改
                if (nRecordCount == 1)
                {
                    SuperMapLib.IsoGeometry pCurrGeo = null;
                    pCurrGeo = pCurrRecordSet.GetGeometry();
                    if (pCurrGeo.Type == SuperMapLib.seGeometryType.scgCustom)
                    {
                        SuperMapLib.IsoGeoCustom pGeoCustom = (SuperMapLib.IsoGeoCustom)pCurrGeo;
                        //soRect s0 = pGeoCustom.Bounds;
                        //soGeoPoint p1 = new soGeoPoint();
                        //soGeoPoint p2 = new soGeoPoint();
                        //soGeoPoint p3 = new soGeoPoint();
                        //soGeoPoint p4 = new soGeoPoint();
                        //p1.x = s0.Left;
                        //p1.y = s0.Bottom;

                        //p2.x = s0.Right;
                        //p2.y = s0.Bottom;
                        //p3.x = s0.Right;
                        //p3.y = s0.Top;
                        //p4.x = s0.Left;
                        //p4.y = s0.Top;
                        //soStyle st = new soStyle();
                        //st.SymbolStyle = 2360;
                        //st.SymbolSize = 200;
                        //p1.Style = st;
                        //p2.Style = st;
                        //p3.Style = st;
                        //p4.Style = st;
                        //objTrackingLayer = this.axSuperMap1.TrackingLayer;
                        //objTrackingLayer.AddEvent(p4 as soGeometry, null, "");
                        //objTrackingLayer.AddEvent(p3 as soGeometry, null, "");
                        //objTrackingLayer.AddEvent(p2 as soGeometry, null, "");
                        //objTrackingLayer.AddEvent(p1 as soGeometry, null, "");
                        //objTrackingLayer.Refresh();
                        MGPPROPLISTLib.ISmExtObj objExt = (MGPPROPLISTLib.ISmExtObj)pGeoCustom.CustomObjInterface;
                        if (objExt != null)
                        {
                            this.axMGPPropList1.FillProperty(objExt);
                        }
                        Marshal.ReleaseComObject(pGeoCustom);
                        Marshal.ReleaseComObject(objExt);
                    }
                    Marshal.ReleaseComObject(pCurrGeo);
                }
                else
                {
                    this.axMGPPropList1.SetDefault();
                }
                Marshal.ReleaseComObject(pCurrRecordSet);
                Marshal.ReleaseComObject(pCurrSel);
                this.axMGPPropList1.Refresh();
            }
            catch (Exception)
            {
            }
        }

        void axSuperMap1_MouseWheelEvent(object sender, AxSuperMapLib._DSuperMapEvents_MouseWheelEvent e)
        {
            if (e.zDelta > 0)
                axSuperMap1.Zoom(1.5);

            if (e.zDelta < 0)
                axSuperMap1.Zoom(1 / 1.5);

            axSuperMap1.Refresh();
        }
        private void SetPosition(double x, double y, double veSacle)
        {
            IPlane5 ip = TE as IPlane5;

            double xx = 0;
            object x1 = (object)xx;
            double yy = 0;
            object y1 = (object)yy;

            double aa = 0;
            object a = (object)aa;

            double ss = 0;
            object s = (object)ss;

            double dd = 0;
            object d = (object)dd;
            double ff = 0;
            object f = (object)ff;

            double gg = 0;
            object g = (object)gg;
            double hh = 0;
            object h = (object)hh;
            double aaa = 1 / veSacle / 100 * (1280 / 24 * 0.635);
            if (ip != null)
            {
                ip.GetPosition(out x1, out y1, out a, out  s, out d, out f, out  g, out h);
                ip.SetPosition(x, y, aaa, (double)s, (double)d, (double)f, (double)g, (double)h, 0);

            }
        }

        void gtimer_Tick(object sender, EventArgs e)
        {
            if (liandong)
            {
                IPlane5 ip = TE as IPlane5;
                double xx = 9999;
                object x = (object)xx;
                double yy = 9999;
                object y = (object)yy;

                double aa = 0;
                object a = (object)aa;

                double ss = 0;
                object s = (object)ss;

                double dd = 0;
                object d = (object)dd;
                double ff = 0;
                object f = (object)ff;

                double gg = 0;
                object g = (object)gg;
                double hh = 0;
                object h = (object)hh;
                if (ip != null)
                {
                    ip.GetPosition(out x, out y, out a, out  s, out  d, out  f, out  g, out  h);

                }
                xx = (double)x;
                yy = (double)y;
                hh = (double)a;

                //TimeSpan tws = date2 - date1;
                //
                if ((mapx != xx || mapy != yy || hh != ascale))
                {
                    if (skywithout)
                    {
                        skywithout = !skywithout;
                    }
                    else
                    {
                        this.supwithout = true;
                        axSuperMap1.CenterX = (double)x;
                        axSuperMap1.CenterY = (double)y;
                        axSuperMap1.ViewScale = 1 / hh / 100 * (1280 / 24 * 0.635) * 111000;
                        //double  ; = 1 / veSacle / 
                        axSuperMap1.Refresh();
                        //date1 = DateTime.Now;
                    }
                    mapx = xx;
                    mapy = yy;
                    ascale = hh;
                }

                //
            }
        }

        /// <summary>
        /// 二维展示
        /// </summary>
        public void Show2D()
        {
            //gtimer.Stop();
            modaltype = 1;
            this.axTE3DWindow1.Visible = false;
            axSuperMap1.Visible = true;
            axSuperMap1.Dock = DockStyle.Fill;
            
        }

        /// <summary>
        /// 三维展示
        /// </summary>
        public void Show3D()
        {
            //gtimer.Stop();
            modaltype = 2;
            axSuperMap1.Visible = false;
            this.axTE3DWindow1.Visible = true;
            this.axTE3DWindow1.Dock = DockStyle.Fill;
        }

        public void Show2DAnd3D()
        {
            gtimer.Start();
            modaltype = 3;
            this.axTE3DWindow1.Dock = DockStyle.None;
            axSuperMap1.Dock = DockStyle.None;
            this.axTE3DWindow1.Visible = true;

            axSuperMap1.Location = new Point(0, 0);            
            axSuperMap1.Visible = true;
            axSuperMap1.Size = new Size(this.axTE3DWindow1.Parent.Size.Width / 2, this.axTE3DWindow1.Parent.Height);

            axSuperMap1.Refresh();
            //double blx = 0;
            //double bly = 0;
            //bltomi(119, 24, out blx, out bly);
            axSuperMap1.ViewScale = 0.0000001 * 111000;
            axSuperMap1.Refresh();
            axSuperMap1.CenterX = 119;
            axSuperMap1.CenterY = 24;
            axSuperMap1.Refresh();
            this.axTE3DWindow1.Location = new Point(this.axTE3DWindow1.Parent.Size.Width / 2 + 1, 0);
            this.axTE3DWindow1.Size = new Size(this.axTE3DWindow1.Parent.Size.Width / 2, this.axTE3DWindow1.Parent.Height);


        }

        #endregion 
     
        /// <summary>
        /// 页面设置
        /// </summary>
        private void btn2D_Click(object sender, EventArgs e)
        {
            Show2D();
            //2D图大小位置
            this.axSuperMap1.Dock = DockStyle.None;
            this.axSuperMap1.Visible = true;
            this.axSuperMap1.Location = new Point(300, 0);
            this.axSuperMap1.Size = new Size(this.axTE3DWindow1.Parent.Size.Width - 300, this.axTE3DWindow1.Parent.Height);
            
            this.btnCez.Enabled = false;
            this.btnJB.Enabled = true;

        }

        private void btn3D_Click(object sender, EventArgs e)
        {
            //IPlane.FlyToObject(IInformationTree.FindItem("index") + "", 0);
            Show3D();
            size3D();
        }
        
        //3D窗口大小
        private void size3D()
        {
            this.axTE3DWindow1.Dock = DockStyle.None;
            this.axTE3DWindow1.Visible = true;
            this.axTE3DWindow1.Location = new Point(300, 0);
            this.axTE3DWindow1.Size = new Size(this.axTE3DWindow1.Parent.Size.Width - 300, this.axTE3DWindow1.Parent.Height + 15);

            this.panel1.Visible = false;
            //this.sideBar.Visible = true;
            this.panel2.Visible = false;

            this.btnCez.Enabled = true;
            this.btnJB.Enabled = false;

        }

        //军标窗口位置
        private void junbiao()
        {
            this.axMGPPropList1.Visible = true;
            this.axMGPSymbolPanelCtrl1.Visible = true;
            this.axMGPSymbolTreeCtrl1.Visible = true;
            this.axMGPSymbolTreeCtrl1.Location = new Point(0, 0);
            this.axMGPSymbolTreeCtrl1.Height = panelEx1.Height / 3;
            this.axMGPSymbolPanelCtrl1.Location = new Point(0, panelEx1.Height / 3);
            this.axMGPSymbolPanelCtrl1.Height = panelEx1.Height / 3;
            this.axMGPPropList1.Location = new Point(0, (panelEx1.Height / 3) * 2);
            this.axMGPPropList1.Height = panelEx1.Height / 3 - 10;
        }

        //军标添加按钮
        private void btnJB_Click(object sender, EventArgs e)
        {
            junbiao();
            if (panel1.Visible==false)
            {
                panel1.Visible = true;
            }
            else
            {
                panel1.Visible = false;
            }
        }

        //视频播放
        private void player()
        {
            axWindowsMediaPlayer1.Location = new Point(300,0);
            axWindowsMediaPlayer1.Visible = true;
            axWindowsMediaPlayer1.settings.volume = 0;
            //axWindowsMediaPlayer1.uiMode = "none";
            axWindowsMediaPlayer1.URL=@"d:\end.wmv";
        }

        
        //想定一
        private void buttonItem68_Click(object sender, EventArgs e)
        {
            Show3D();
            size3D();
            label1.Visible = true;
            panel2.Visible = true;
            treeView1.Visible = false;
            System.IO.StreamReader reader = new System.IO.StreamReader(Application.StartupPath + @"\media\yuyin\sound11.txt", System.Text.Encoding.Default);
            System.IO.StreamReader reader1 = new System.IO.StreamReader(Application.StartupPath + @"\media\yuyin\sound1.txt", System.Text.Encoding.Default);
            string content = reader.ReadToEnd();
            string content1 = reader1.ReadToEnd();
            richTextBox1.Text = content1;
            label1.Text = "想定方案";
            label2.Visible = false;
            richTextBox2.Visible = false;
            if (MessageBox.Show("是否进行语音和视频播放?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //btn3D.Enabled = false;
                //btn2D.Enabled = false;
                axWindowsMediaPlayer1.Visible = true;
                player();
                label.EnableMarquee = true;
                label.Visible = true;
                Regex reg = new Regex(@"\r\n");
                string str = reg.Replace(content, "");
                label.Caption = str;
                speech sp = new speech();
                sp.Speak(content);
                
            }
            reader.Close();
            
            IPlane.FlyToObject(IInformationTree.FindItem("化工厂") + "", 0);  
        }

        //补充想定-1
        private void buttonItem2_Click(object sender, EventArgs e)
        {
            Show3D();
            size3D();
            label1.Visible = true;
            panel2.Visible = true;
            treeView1.Visible = false;
            System.IO.StreamReader reader1 = new System.IO.StreamReader(Application.StartupPath + @"\media\yuyin\sound11.txt", System.Text.Encoding.Default);
            System.IO.StreamReader reader2 = new System.IO.StreamReader(Application.StartupPath + @"\media\yuyin\sound2.txt", System.Text.Encoding.Default);
            System.IO.StreamReader reader3 = new System.IO.StreamReader(Application.StartupPath + @"\media\yuyin\sound1.txt", System.Text.Encoding.Default);

            string content = reader1.ReadToEnd() + "\n"+reader2.ReadToEnd();
            string content1 = reader3.ReadToEnd();
            richTextBox1.Text = content1;
            label1.Text = "想定方案";
            label2.Visible = false;
            richTextBox2.Visible = false;
            if (MessageBox.Show("是否进行语音和视频播放?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                btn3D.Enabled = false;
                btn2D.Enabled = false;
                player();
                label.EnableMarquee = true;
                label.Visible = true;
                Regex reg = new Regex(@"\r\n");
                string str = reg.Replace(content, "");
                label.Caption = str;
                speech sp = new speech();
                sp.Speak(content);
            }
            reader2.Close();
            reader1.Close();
            reader3.Close();
            IPlane.FlyToObject(IInformationTree.FindItem("化工厂") + "", 0); 
        }
      
        //想定二
        private void buttonItem72_Click(object sender, EventArgs e)
        {
           
        }
        //想定三
        private void buttonItem47_Click(object sender, EventArgs e)
        {
           
        }


        //资料查询
        searchForm sef = null;
        private void buttonItem44_Click(object sender, EventArgs e)
        {

            if (sef == null || sef.IsDisposed)
            {
                sef = new searchForm();
                sef.Show();
            }
            else
            {
                sef.Activate();
            }
        }

        //试题添加
        private void btnTJST_Click(object sender, EventArgs e)
        {
            jxgl.TJSTForm tf = new HYWJ.jxgl.TJSTForm();
            tf.ShowDialog();
        }

        //考试
        private void btnMNKS_Click(object sender, EventArgs e)
        {
            TXXZForm txf = new TXXZForm();
            txf.ShowDialog();
        }

        //流程按钮
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text=="方案部署开始")
            {
                FABSForm faf = new FABSForm();
                axWindowsMediaPlayer1.Ctlcontrols.stop();
                axWindowsMediaPlayer1.Visible = false;
                faf.Show();
                label.EnableMarquee = false;
                label.Visible = false;
                button1.Text = "力量部署开始";
            }
            else if (button1.Text == "力量部署开始")
            {
                Show2D();
                junbiao();
                //2D图大小位置
                this.axSuperMap1.Dock = DockStyle.None;
                this.axSuperMap1.Visible = true;
                this.axSuperMap1.Location = new Point(300, 0);
                this.axSuperMap1.Size = new Size(this.axTE3DWindow1.Parent.Size.Width - 300, this.axTE3DWindow1.Parent.Height);

                this.btnCez.Enabled = false;
                this.btnJB.Enabled = true;
                this.panel1.Visible = true;

                label1.Text = "情况分析";
                label2.Visible = true;
                richTextBox2.Visible = true;
                open1(Application.StartupPath + @"\提交文档\情况分析\1.txt");
                open2(Application.StartupPath + @"\提交文档\决心制定\1.txt");

                button1.Text = "部署完毕,结果评估";
            }
            else if (button1.Text == "部署完毕,结果评估")
            {
                if (MessageBox.Show("提交标绘信息并进行下一步？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    label1.Visible = true;
                    JGPGForm jf = new JGPGForm();
                    jf.ShowDialog();

                    panel2.Visible = false;
                    treeView1.Visible = true;
                    button1.Visible = true;
                    button1.Text = "方案部署开始";
                    btn3D.Enabled = true;
                    btn2D.Enabled = true;
                    Show3D();
                    size3D();
                    btnCez.Enabled = true;
                    btnJB.Enabled = false;
                }    
            } 
        }

        //richbox1显示情况分析
        private void open1(string file)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(file, System.Text.Encoding.Default);
            string content = reader.ReadToEnd();
            richTextBox1.Text = content;
            reader.Close();
        }
        
        //richbox2显示决心
        private void open2(string file)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(file, System.Text.Encoding.Default);
            string content = reader.ReadToEnd();
            richTextBox2.Text = content;
            reader.Close();
        }
       
        #region 教学管理
        //态势管理
        private void btnTSGL_Click(object sender, EventArgs e)
        {
            jxgl.TSGLForm tsf = new HYWJ.jxgl.TSGLForm();
            tsf.ShowDialog();
        }
        //作业管理
        private void btnZYGL_Click(object sender, EventArgs e)
        {
            jxgl.ZYGLForm zyf = new HYWJ.jxgl.ZYGLForm();
            zyf.ShowDialog();
        }
        //资料管理
        private void btnZLGL_Click(object sender, EventArgs e)
        {
            jxgl.ZLGLForm zf = new HYWJ.jxgl.ZLGLForm();
            zf.ShowDialog();
        }
        //辅助评估
        private void btnFZPG_Click(object sender, EventArgs e)
        {
            jxgl.FZPGForm ff = new HYWJ.jxgl.FZPGForm();
            ff.ShowDialog();
        }
        //题库管理
        private void btnTKGL_Click(object sender, EventArgs e)
        {
            jxgl.TKGLForm tf = new HYWJ.jxgl.TKGLForm();
            tf.ShowDialog();
        }

        #endregion

        //treeview
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode.Text.ToString() == "想定一")
            {
                if (MessageBox.Show("进行'想定一'模拟训练？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    buttonItem68_Click(null, null);
                }
            }
            else if (treeView1.SelectedNode.Text.ToString() == "补充想定-1")
            {
                if (MessageBox.Show("进行'补充想定-1'模拟训练？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    buttonItem2_Click(null, null);
                }
            }
            else if (treeView1.SelectedNode.Text.ToString() == "态势管理")
            {
                btnTSGL_Click(null, null);
            }
            else if (treeView1.SelectedNode.Text.ToString() == "题库管理")
            {
                btnTKGL_Click(null, null);
            }
            else if (treeView1.SelectedNode.Text.ToString() == "添加试题")
            {
                btnTJST_Click(null, null);
            }
            else if (treeView1.SelectedNode.Text.ToString() == "应急训练考试")
            {
                btnMNKS_Click(null, null);
            }
            else if (treeView1.SelectedNode.Text.ToString() == "辅助评估")
            {
                btnFZPG_Click(null, null);
            }
            else if (treeView1.SelectedNode.Text.ToString() == "学员作业管理")
            {
                btnZYGL_Click(null, null);
            }
            else if (treeView1.SelectedNode.Text.ToString() == "资料查询")
            {
                buttonItem44_Click(null, null);
            }
            else if (treeView1.SelectedNode.Text.ToString() == "多媒体资料管理")
            {
                btnZLGL_Click(null, null);
            }
            else if (treeView1.SelectedNode.Text.ToString() == "关于系统")
            {
                btnAbout_Click(null, null);
            }

        }

        //军标库收起下拉
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (panel1.Height>10)
            {
                panel1.Height = 10;
                Image img = Image.FromFile(Application.StartupPath + @"\images\xiala.jpg");
                pictureBox1.Image = img;

            }
            else if (panel1.Height<=16)
            {

                panel1.Height = panelEx1.Height;
                junbiao();
                Image img = Image.FromFile(Application.StartupPath + @"\images\shouqi.jpg");
                pictureBox1.Image = img;
            }
        }


        //打开定位
        private void btnPosition_Click(object sender, EventArgs e)
        {
            panel3.Visible = true;
        }
        //关闭定位
        private void button3_Click(object sender, EventArgs e)
        {
            this.panel3.Visible = false;
        }

        //定位modaltype  2=3D  1=2D
        private void btnGPS_Click(object sender, EventArgs e)
        {
            double x, y;
            x = Convert.ToDouble(num1.Value);
            x = x + Convert.ToDouble(num2.Value) / 60;
            x = x + Convert.ToDouble(num3.Value) / 3600;
            x = Convert.ToDouble(x.ToString("f6"));


            y = Convert.ToDouble(num4.Value);
            y = y + Convert.ToDouble(num5.Value) / 60;
            y = y + Convert.ToDouble(num6.Value) / 3600;
            y = Convert.ToDouble(y.ToString("f6"));
            if (modaltype == 2)
            {
                IPlane.FlyTo(x, y, 5000, 1000, 0, 0, "FlyToTerrain");
            }
            else if (modaltype == 1)
            {
                axSuperMap1.CenterX = x;
                axSuperMap1.CenterY = y;
                axSuperMap1.Refresh();
            }
        }

        private void MianForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label.Transparency = trackBar1.Value;
        }

    }
}
